<?php
include '../../../wp-blog-header.php';

$url = 'http://www.affilired.com/master-offer-feed.php';
function download_page($path){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$path);
        curl_setopt($ch, CURLOPT_FAILONERROR,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        $retValue = curl_exec($ch);                      
        curl_close($ch);
        return $retValue;
}

$curl_json_output = download_page($url);
$affilired_deals = xml2array($curl_json_output);
shuffle($affilired_deals['Offers']['Offer']);
$affilired_daily_deals = array_slice($affilired_deals['Offers']['Offer'], 0, 1);
//echo "<pre>";
//print_r($affilired_daily_deals);
//exit();

foreach($affilired_daily_deals as $key => $value)
{
$columns_deal['name'] = pods_sanitize($value['offer_title']);
$columns_deal['camp_link'] = substr(sanitize_title($columns_deal['name']),0,128);
$columns_deal['short'] = pods_sanitize($value['offer_description']);
$ext_img_url = $value['image'];
 
$deal = new Pod('deal');
$where = "t.camp_link = '".$columns_deal['camp_link']."'";
$deal->findRecords('id DESC', 1,$where);
$dealsFound = $deal->getTotalRows();
//echo $dealsFound; 

if($dealsFound == 0)
{
  if(strtotime($value['offer_startdate']) <= strtotime(date('Y-m-d 12:00:00'))){
  
  unset($ids);
  unset($current_cities);
  $City = new Pod('city');
	$City->findRecords('name ASC', -1);
	while($City->fetchRecord())
	{
  	$ids  = $City->get_field('id');
  	$current_cities[] = $ids;
	}
  $current_cities = implode(',',$current_cities);
  $columns_deal['city'] = $current_cities;
  
  $tags_pod = new Pod('tags');
	$tags_pod->findRecords('name ASC', -1,"t.slug IN('t-best-international-hotels','t-international-hotels')");
	while($tags_pod->fetchRecord())
	{
  	$tag_id = $tags_pod->get_field('id');
    $tag_ids[] = $tag_id; 
	}
	$tag_id = implode(',',$tag_ids);
  $columns_deal['tag_pick'] = $tag_id;
  
  $adnid = $value['adnid'];
  $off_url = $value['offer_url'];
  $extlink = 'http://scripts.affilired.com/?adnid='.$adnid.'&adn=3990&url='.$off_url;
  $shortened_url = make_bitly_url($extlink);
  if(empty($shortened_url))
  {
    $shortened_url = make_goog_url($extlink);
  }
  $columns_deal['extlink'] = $shortened_url;
  
  $columns_deal['quickcamp'] = 1;
  $columns_deal['dealcheck'] = 1;
  $columns_deal['camp_end'] = date('Y-m-d 12:00:00',strtotime($value['offer_enddate']));
  $columns_deal['camp_start'] = date('Y-m-d 12:00:00',strtotime($value['offer_startdate']));
  $columns_deal['deal_address'] = implode(', ',$value['hotel_destinations']['LEVEL']);
  $columns_deal['brand'] = $brand_id;
  $columns_deal['ext_image_url'] = $ext_img_url;

  $category_pod = new Pod('category');
	$category_pod->findRecords('name ASC', -1,"t.url = 'c-travel-offers'");
	while($category_pod->fetchRecord())
	{
  	$category_id  = $category_pod->get_field('id');
	}
  $columns_deal['camp_cat'] = $category_id;

  $brand = new Pod('brand');
  $brand->page = 1;
  $brand->search = false;
  $where = "t.brandurl = '".sanitize_title($value['hotel_chain'])."'";
  $brand->findRecords('id DESC', -1,$where);
  $brandFound = $brand->getTotalRows();
  
  if($brandFound != 1)
  {
    $api_brand = new PodAPI();
    $columns_brand['name'] = pods_sanitize($value['hotel_chain']);
    $columns_brand['brandurl'] = sanitize_title($columns_brand['name']);
    $columns_brand['local'] = '1';
    $params_brand = array('datatype' => 'brand', 'columns' => $columns_brand);
    $brand_id = $api_brand->save_pod_item($params_brand);
  }
  else
  {
    while($brand->fetchRecord())
    {
      $brand_id = $brand->get_field('id');
    }
  }
  $columns_deal['brand'] = $brand_id;


  $api = new PodAPI();
  $params = array('datatype' => 'deal', 'columns' => $columns_deal);
  
  //echo '<pre>';
  //print_r($params);
  //echo '</pre>';
  //exit();
  
  $api->save_pod_item($params);
}

}

}